# -*- coding: utf-8 -*

import config
import datastore

import os

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template


class IndexPage(webapp.RequestHandler):
    def get(self):
        query = datastore.HotSpotCategory.all()
        categories = query.fetch(config.MAX_SPOT_CATEGORY_NUM)

        query = datastore.HotSpot.gql('WHERE name = :1', config.HOME_SPOT_NAME)
        home = query.fetch(1)
        if len(home) == 1:
            home = home[0]
        else:
            home = { 'key': '', 'name': '' }

        spots_list = []
        for category in categories:
            spots = category.spot_set.fetch(config.MAX_SPOT_LIST_NUM)
            spots_list.append({'name': category.name, 'spots': spots })

        templateDict = {
            'home': home,
            'spots_list': spots_list,
        }

        path = os.path.join(os.path.dirname(__file__), 'index.html')
        self.response.out.write(template.render(path, templateDict))
